<template>
	<view>
		<view class="search-box">
			<my-search @click='toSearch'></my-search>
		</view>
		<view class="scroll-view-container">
			<!-- 左侧的滚动视图区域 -->
			<scroll-view class="left-scroll-view" scroll-y :style="{height: wh + 'px'}">
				<view :class="['left-scroll-view-item',index===active?'active':'']" v-for="(item,index) in cateList"
					:key='item.cat_id' @click="changeActive(index)">{{item.cat_name}}</view>
			</scroll-view>
			<!-- 右侧的滚动视图区域 -->
			<scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}">
				<view class="right-scroll-view-item" v-for="item in cateLevel2" :key='item.cat_id'>
					<view class="title">
						{{item.cat_name}}
					</view>
					<view class="goods">
						<navigator class="goods-item" v-for="item1 in item.children" :key='item1.cat_id'
							:url='"/subpkg/goods_list/goods_list?cid="+item1.cat_id'>
							<image :src="item1.cat_icon" class="floor-title" mode='widthFix'></image>
							<text>{{item1.cat_name}}</text>
						</navigator>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				wh: 0,
				cateList: [],
				cateLevel2: [],
				active: 0
			};
		},
		onLoad() {
			let systemInfo = uni.getSystemInfoSync()
			this.wh = systemInfo.windowHeight - 50

			this.getCateList()
		},
		methods: {
			async getCateList() {
				// 发起请求
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/categories')
				// 判断是否获取失败
				if (res.meta.status !== 200) {
					return uni.showToast({
						title: '数据请求失败！',
						duration: 1500,
						icon: 'none',
					})
				}
				console.log(res);
				// 转存数据
				this.cateLevel2 = res.message[0].children
				this.cateList = res.message
			},
			changeActive(i) {
				this.active = i
				this.cateLevel2 = this.cateList[i].children
			},
			toSearch() {
				console.log(6666);
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		.left-scroll-view {
			width: 120px;

			.left-scroll-view-item {
				line-height: 60px;
				text-align: center;
				font-size: 12px;
				background-color: #f7f7f7;

				&.active {
					background-color: #fff;
					position: relative;

					&::before {
						content: '';
						display: block;
						width: 3px;
						height: 30px;
						background-color: #c00000;
						position: absolute;
						left: 0;
						top: 50%;
						transform: translateY(-50%);
					}
				}

			}
		}

		.right-scroll-view-item {
			.title {
				text-align: center;
				font-size: 16px;
				color: #333;
				padding: 20rpx 0;
				font-weight: 900;
			}

			.goods {
				display: flex;
				flex-wrap: wrap;
				border-bottom: 1px dashed #ddd;
				padding-bottom: 20rpx;

				.goods-item {
					width: 33.33%;
					display: flex;
					flex-direction: column;
					align-items: center;

					image {
						width: 60px;
						height: 60px;
					}
				}
			}
		}
	}
</style>
